package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.ObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RequestMappingHandlerMapping rmhm;

    public void save(Permission permission) {

        permissionMapper.insert(permission);
    }

    public void delete(Long id) {
        if (id != null) {
            permissionMapper.deleteByPrimaryKey(id);

        }
    }

    public void update(Permission permission) {

        permissionMapper.updateByPrimaryKey(permission);
    }

    public Permission get(Long id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    public List<Permission> getAll() {

        return permissionMapper.selectAll();
    }

    public PageResult<Permission> query(ObjectQuery qo) {
        int totalCount = permissionMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult<Permission>(totalCount, Collections.<Permission>emptyList());
        }


        return new PageResult<Permission>(totalCount,permissionMapper.queryForList(qo));
    }

    public PageResult<Permission> queryPermissionByRoleId(Long roleId) {
        List list = permissionMapper.queryPermissionByRoleId(roleId);
        return new PageResult<>(list.size(),list);
    }

    public void reload() {
        //0:排重
        List<Permission> permissions = permissionMapper.selectAll();
        HashSet<Object> set = new HashSet<>();
        for (Permission permission : permissions) {
            set.add(permission.getResource());
        }
        //获取到所有方法
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();

        for (HandlerMethod hm : values) {

            RequiresPermissions anno = hm.getMethodAnnotation(RequiresPermissions.class);
            if (anno==null) {
                continue;
            }
            String resource = anno.value()[0];

            //如果存在跳过
            if (set.contains(resource)) {
                continue;
            }
            if (hm.getMethodAnnotation(PermissionName.class)==null){
                continue;
            }
            //获取权限名称
            String name = hm.getMethodAnnotation(PermissionName.class).value();
            cn.wolfcode.crm.domain.Permission permission = new cn.wolfcode.crm.domain.Permission();
            permission.setName(name);
            permission.setResource(resource);
            permissionMapper.insert(permission);

        }
    }

    public List<String> queryPermissionResourceByEmpId(Long id) {
        return permissionMapper.queryPermissionResourceByEmpId(id);
    }


}
